//
//  UiManager.m
//  iRMAP
//
//  Created by Ryan Enguero on 4/2/14.
//  Copyright (c) 2014 engueroryan. All rights reserved.
//

#import "UiManager.h"


#import "CPDScatterPlotViewController.h"

#import "MainPageViewController.h"
#import "DevicesTableViewController.h"
#import "DevicesViewController.h"
#import "AlarmsTableViewController.h"
#import "SupportViewContoller.h"

#import "DeviceInfoViewController.h"


#import "GraphViewController.h"
#import "MeasurementViewController.h"
#import "SetPointViewController.h"
#import "LogsViewController.h"

#import "IIViewDeckController.h"
#import "GraphDynamicViewController.h"


static UiManager *sharedItem = nil;
//IIViewDeckController* deckController;
//IIViewDeckController* deckControllerGraph;

@implementation UiManager

#pragma mark - Main Tabs Initialize

- (UITabBarController*)mainTabsInitialize{

    
    self.mainPageViewController = [[MainPageViewController alloc] initWithNibName:@"MainPageViewController" bundle:nil];
    
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    
    self.devicesTableView = [[DevicesTableViewController alloc] initWithNibName:@"DevicesTableViewController" bundle:nil];
    
    self.supportViewCon = [[SupportViewContoller alloc] initWithNibName:@"SupportViewContoller" bundle:nil];
//    self.devicesView = [[DevicesViewController alloc] initWithNibName:@"DevicesViewController" bundle:nil];    
    UINavigationController *navBarDevices = [[UINavigationController alloc] initWithRootViewController:self.devicesTableView];
    
    self.alarmsTableView = [[AlarmsTableViewController alloc] initWithNibName:@"AlarmsTableViewController" bundle:nil];
    
    UINavigationController *navBarAlarms = [[UINavigationController alloc] initWithRootViewController:self.alarmsTableView];
    
    tabBar.viewControllers = @[navBarDevices,navBarAlarms,[self initializeMailController]];
    tabBar.selectedViewController = navBarDevices;

    return tabBar;
}

- (IIViewDeckController*)generateControllerStack{


    self.mainPageViewController = [[MainPageViewController alloc] initWithNibName:@"MainPageViewController" bundle:nil];
    
    UINavigationController *navBarCompany = [[UINavigationController alloc] initWithRootViewController:self.mainPageViewController];
    
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    
    self.devicesTableView = [[DevicesTableViewController alloc] initWithNibName:@"DevicesTableViewController" bundle:nil];
    
    self.devicesView = [[DevicesViewController alloc] initWithNibName:@"DevicesViewController" bundle:nil];
    
    self.supportViewCon = [[SupportViewContoller alloc] initWithNibName:@"SupportViewContoller" bundle:nil];
    
    UINavigationController *navBarDevices = [[UINavigationController alloc] initWithRootViewController:self.devicesTableView];
    
    self.alarmsTableView = [[AlarmsTableViewController alloc] initWithNibName:@"AlarmsTableViewController" bundle:nil];
    
    UINavigationController *navBarAlarms = [[UINavigationController alloc] initWithRootViewController:self.alarmsTableView];
    
    self.supportViewCon = [[SupportViewContoller alloc] initWithNibName:@"SupportViewContoller" bundle:nil];
    
    [navBarDevices.tabBarItem setImage:[UIImage imageNamed:@"DeviceTab_unselected"]];
    [navBarAlarms.tabBarItem setImage:[UIImage imageNamed:@"LogTab_unselected"]];
    [self.supportViewCon.tabBarItem setImage:[UIImage imageNamed:@"SupportTab_unselected"]];
    
   // [[self initializeMailController].tabBarItem setImage:[UIImage imageNamed:@"SupportTab_unselected"]];
    
    tabBar.viewControllers = @[navBarDevices,navBarAlarms,[self initializeMailController]];
    tabBar.selectedViewController = navBarDevices;
    
    
    
    self.deckController =  [[IIViewDeckController alloc] initWithCenterViewController:tabBar
                                                                                    leftViewController:navBarCompany
                                                                                   rightViewController:nil];
    self.deckController.leftSize = 50;
    
    [self.deckController disablePanOverViewsOfClass:NSClassFromString(@"_UITableViewHeaderFooterContentView")];
    return self.deckController;

}


- (IIViewDeckController*)graphViewDeck{
    
    
        
//    self.graphView = [[GraphDynamicViewController alloc] initWithNibName:@"GraphDynamicViewController" bundle:nil];
    
    self.testGraph = [[CPDScatterPlotViewController alloc] initWithNibName:@"CPDScatterPlotViewController" bundle:nil];
    
    self.graphViewSide = [[GraphViewController alloc] initWithNibName:@"GraphViewController" bundle:nil];
    
    self.deckControllerGraph =  [[IIViewDeckController alloc] initWithCenterViewController:self.testGraph
                                                              leftViewController:nil
                                                             rightViewController:self.graphViewSide];
    self.deckControllerGraph.rightSize = 50;
    
    [self.deckControllerGraph disablePanOverViewsOfClass:NSClassFromString(@"_UITableViewHeaderFooterContentView")];
    return self.deckControllerGraph;
    
}



#pragma mark - Devices

- (UIViewController *)TestGraph{
    
    self.deviceInfoView = [[DeviceInfoViewController alloc] initWithNibName:@"DeviceInfoViewController" bundle:nil];
    
    return self.deviceInfoView;
    
}

- (UIViewController *)deviceInfo{
    
     self.deviceInfoView = [[DeviceInfoViewController alloc] initWithNibName:@"DeviceInfoViewController" bundle:nil];

    return self.deviceInfoView;

}

- (UIViewController *)graphViewController{
    
    self.testGraph = [[CPDScatterPlotViewController alloc] initWithNibName:@"CPDScatterPlotViewController" bundle:nil];

    self.devicesTableView.delegate = self.testGraph;
    
    return self.testGraph;
}

- (UIViewController *)measurementViewController{
    
    self.measurementView = [[MeasurementViewController alloc] initWithNibName:@"MeasurementViewController" bundle:nil];
    
    return self.measurementView;

}

- (UIViewController *)setpointViewController{
    
     self.setpointView = [[SetPointViewController alloc] initWithNibName:@"SetPointViewController" bundle:nil];
    
    return self.setpointView;

}

- (UIViewController *)logsViewController{
    
    self.logsView = [[LogsViewController alloc] initWithNibName:@"LogsViewController" bundle:nil];

    return self.logsView;

}

- (void)slideLeft{

    [self.devicesTableView.tableView reloadData];
    //[self.devicesView.tableView reloadData];
   // [self.alarmsTableView.tableView reloadData];
    
    [self.deckController closeLeftViewAnimated:YES];
    
    
}

- (void)openLeft{
    
    [self.deckController openLeftViewAnimated:YES];


}
- (void)logOut{

    [self.deckController.navigationController popViewControllerAnimated:YES];
     //dismissViewControllerAnimated:YES completion:nil];

}

+ (UILabel*)navigationHeaderWithTitle:(NSString*)headerStr andViewController:(UIViewController*)viewCon{
    
    
    UILabel *titleView = (UILabel *)viewCon.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        
        titleView.textColor = [UIColor whiteColor]; // Change to desired color
        
        viewCon.navigationItem.titleView = titleView;
    }
    titleView.text = headerStr;
    [titleView sizeToFit];
    
    return titleView;
}

#pragma mark - Support View

- (MFMailComposeViewController*)initializeMailController{

    // Email Subject
    NSString *emailTitle = @"";
    // Email Content
    NSString *messageBody = @"";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"software.support@escoglobal.com"];
    
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];
    
    [mc.tabBarItem setImage:[UIImage imageNamed:@"SupportTab_unselected"]];
    mc.title = NSLocalizedString(@"Support", @"Support");
    
    
    return mc;

}

#pragma mark - mail compose delegate
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }
    
    // Close the Mail Interface
    //[self dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark - SingleTon

+ (UiManager *)sharedItems
{
    if (sharedItem == nil) {
        sharedItem = [[super allocWithZone:NULL]init];
    }
    return  sharedItem;
}
- (id)init{
    if ((self = [super init])) {
        
    }
    return self;
}
+ (id)allocWithZone:(NSZone *)zone{
    return  [self sharedItems];
}
- (id)copyWithZone:(NSZone *)zone{
    return self;
}

@end
